package com.example.demo.controller.surperController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.CooperativeEntMapper;
import com.example.demo.Mapper.SurperMapper.CooperativeEntSurperMapper;
import com.example.demo.base.Result;
import com.example.demo.entity.CooperativeEnt;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cooperativeEntSurper")
public class CooperativeEntSurperController {
    @Resource
    CooperativeEntSurperMapper cooperativeEntSurperMapper;

    /**
     * 获取数据库全部数据
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(cooperativeEntSurperMapper.selectList(null));
    }

    /**
     * 分页获取数据库数据
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/pageAll")
    public Result<?> findPageF(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<CooperativeEnt> wrapper = Wrappers.<CooperativeEnt>lambdaQuery();
        Page<CooperativeEnt> cooperativeEntPage = cooperativeEntSurperMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(cooperativeEntPage);
    }

    /**
     * 根据ID精确查找
     *
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result<?> findById(@PathVariable String id) {
        CooperativeEnt cooperativeEnt = cooperativeEntSurperMapper.selectById(id);
        List<CooperativeEnt> list = new ArrayList<>();
        if (cooperativeEnt != null) {
            list.add(cooperativeEnt);
            return Result.success(list);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据企业名称模糊查找
     *
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    public Result<?> findByName(@PathVariable String name) {
        LambdaQueryWrapper<CooperativeEnt> wrapper = Wrappers.<CooperativeEnt>lambdaQuery();
        wrapper.like(CooperativeEnt::getFirmName, name);
        List<CooperativeEnt> list = cooperativeEntSurperMapper.selectList(wrapper);
        if (list.size() > 0)
            return Result.success(list);
        return Result.error(0, "没有查询到相关数据");
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delById/{id}")
    public Result<?> delete(@PathVariable String id) {
        int res = cooperativeEntSurperMapper.deleteById(id);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

    /**
     * 添加信息
     * @param id
     * @param firmName
     * @param needPro
     * @param name
     * @param tel
     * @param location
     * @param note
     * @return
     */
    @GetMapping(value = "/add")
    public Result<?> saveC(@RequestParam String id,
                           @RequestParam String firmName,
                           @RequestParam String needPro,
                           @RequestParam String name,
                           @RequestParam String tel,
                           @RequestParam String location,
                           @RequestParam String note,
                           @RequestParam String tenantId) {
        if (id.isEmpty() || needPro.isEmpty() || name.isEmpty() || tel.isEmpty() || firmName.isEmpty() || location.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        CooperativeEnt cooperativeEnt = new CooperativeEnt(Integer.valueOf(id), firmName, needPro, name, tel, location, note,Integer.valueOf(tenantId));
        cooperativeEntSurperMapper.insert(cooperativeEnt);
        return Result.success();
    }

    /**
     * 修改信息
     * @param id
     * @param firmName
     * @param needPro
     * @param name
     * @param tel
     * @param location
     * @param note
     * @return
     */
    @GetMapping(value = "/update")
    public Result<?> updateById(@RequestParam String id,
                           @RequestParam String firmName,
                           @RequestParam String needPro,
                           @RequestParam String name,
                           @RequestParam String tel,
                           @RequestParam String location,
                           @RequestParam String note,
                                @RequestParam String tenantId) {
        if (id.isEmpty() || needPro.isEmpty() || name.isEmpty() || tel.isEmpty() || firmName.isEmpty() || location.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        CooperativeEnt cooperativeEnt = new CooperativeEnt(Integer.valueOf(id), firmName, needPro, name, tel, location, note,Integer.valueOf(tenantId));
        cooperativeEntSurperMapper.updateById(cooperativeEnt);
        return Result.success();
    }

}
